﻿using System;
using System.Collections.Generic;
using System.Json;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Browser;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using jLight;

namespace jLightTest
{
    public partial class DemoPage2 : UserControl
    {
        public DemoPage2()
        {
            InitializeComponent();
            
            //jQuery.Load needs to be called to load jQuery;
            jQuery.Load();
        }

        /// <summary>
        /// Handles the Click event of the CallButton control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="System.Windows.RoutedEventArgs"/> instance containing the event data.</param>
        private void CallButton_Click(object sender, RoutedEventArgs e)
        {
            //The GetJsonP method can be used to overcome crossdomain issues. 
            jQuery.GetJsonP("http://search.twitter.com/search.json?lang=en&q=sorskoot", Done);
        }

        /// <summary>
        /// When the call to twitter succeeded this method is called.
        /// </summary>
        /// <param name="arg">The twitter data in json format.</param>
        private void Done(JsonValue arg)
        {
            var tweets = new List<Twit>();
            foreach (JsonObject result in arg["results"])
            {
                tweets.Add(new Twit()
                               {
                                   Text = (string)result["text"],
                                   Image = (string)result["profile_image_url"],
                                   User = (string)result["from_user"]
                               }
                    );
            }
            TwitList.ItemsSource = tweets;
        }
    }

    /// <summary>
    /// Helper class to store twitter data
    /// </summary>
    public class Twit
    {
        public string User { get; set; }
        public string Image { get; set; }
        public string Text { get; set; }
    }
}
